import "./public-path";
import Vue from "vue";
import "normalize.css/normalize.css"; // A modern alternative to CSS resets

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import locale from "element-ui/lib/locale/lang/en"; // lang i18n

import "@/styles/index.scss"; // global css

import App from "./App";
import store from "./store";
import router from "./router";

import "@/icons"; // icon
import "@/permission"; // permission control

// 自动更新提醒，仅在生产环境使用
import autoUpdate from "@/utils/auto-update";
process.env.NODE_ENV !== "development" && autoUpdate.start();

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === "production") {
  const { mockXHR } = require("../mock");
  mockXHR();
}

import Print from "vue-print-nb";

// set ElementUI lang to EN
Vue.use(ElementUI, { locale });
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
Vue.use(Antd);

import TAntdui from "@wocwin/t-antd-ui";

// using ES6 modules
import VueSplit from "vue-split-panel";

import "./registerServiceWorker";
Vue.use(VueSplit);

Vue.use(TAntdui);
Vue.config.productionTip = false;

// 与基座进行数据交互
function handleMicroData() {
  // 是否是微前端环境
  if (window.__MICRO_APP_ENVIRONMENT__) {
    // 主动获取基座下发的数据
    console.log("child-vue2 getData:", window.microApp.getData());

    // 监听基座下发的数据变化
    window.microApp.addDataListener((data) => {
      console.log("child-vue2 addDataListener:", data);

      // 当基座下发path时进行跳转
      if (data.path && data.path !== router.currentRoute.path) {
        router.push(data.path);
      }
    });

    // 向基座发送数据
    setTimeout(() => {
      window.microApp.dispatch({ myname: "child-vue2" });
    }, 3000);
  }
}

Vue.use(Print);
const app = new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");

console.log("微应用渲染了");
handleMicroData();

// 监听卸载操作
window.addEventListener("unmount", function () {
  app.$destroy();
  console.log("微应用卸载了");
});
